#!/usr/bin/python
# -*- coding:utf8-*-

import redis

'''
初始化redis数据
'''


class RedisWriter:
    def __init__(self):
        self.conn = redis.Redis(host='localhost', port=6379)
        self.pipe = self.conn.pipeline(transaction=False)


    def run(self):
        # self.write_uid_lis()
        # self.write_userinfo()
        self.write_tokenids()

    def write_tokenids(self):
        for i in xrange(1, 100 * 10000 + 1):
            if len(str(i)) < 8:
                j = 7 - len(str(i))
                k = j * '0' + str(i)
                print k
                self.conn.sadd('tokenids', k)

    def write_uid_lis(self):
        self.conn.sadd('uids', 'uid1', 'uid2', 'uid3', 'uid4', 'uid5')

    def write_userinfo(self):
        uids = self.conn.smembers('uids')
        print uids
        for i in xrange(1, 100 * 10000 + 1):
            if len(str(i)) < 8:
                j = 7 - len(str(i))
                k = j * '0' + str(i)
                print k
                dic = {
                    'tokenId': k,
                    'status': 'OK',
                    'count': 0,
                    'userInfo': 'xxxxxxxxxxxxxxxxxxxx'
                }
                self.pipe.hmset(k, dic)
                for uid in uids:
                    self.pipe.sadd('tid_avaliable_' + str(uid), k)
                self.pipe.execute()

    def __del__(self):
        self.pipe.execute()



if __name__ == '__main__':
    runner = RedisWriter()
    runner.run()
